package com.lzx.adx.report.util.helper;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.proxy.ProxyReq;
import com.lzx.adx.report.model.tencent.request.TencentReportRequest;
import com.lzx.adx.report.util.MacroReplacement;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class ProxyHelper {


    public static String handUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        switch (accountDTO.getProductChannel()) {
            case ProductChannelConstant.KK:
                log.warn("builderKKurl");
                return KkHeper.covertXykkUrl(reportDTO,accountDTO);
            case ProductChannelConstant.JDJR:
                log.warn("builderjdjrurl");
                return builderJdjrUrl(reportDTO, accountDTO);
            case ProductChannelConstant.XUAN_YI:
                log.warn("xy");
                return UrlUtil.covertAllXuanYiUrl(accountDTO, reportDTO);
            case ProductChannelConstant.YTY:
                log.warn("yty");
                return YtyHeper.covertYouYouUrl(accountDTO, reportDTO);
            case ProductChannelConstant.YOU_DAO:
                log.warn("yd");
                return KkHeper.covertAllYDUrl(accountDTO, reportDTO);
            case ProductChannelConstant.YEIYU:
                log.warn("yeyu");
                return KkHeper.covertYeYu(accountDTO, reportDTO);
            case ProductChannelConstant.CZ:
                log.warn("cz");
                return KkHeper.covertCz(accountDTO, reportDTO);
            case ProductChannelConstant.JYM:
                log.warn("jym");
                return UrlUtil.covertAllJymUrl(accountDTO, reportDTO);
            default:
                log.warn("Unknown");
                return "Unknown";
        }
    }

    public static String builderKKUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("adGroupld", reportDTO.getAdgroupId());
        params.put("campaignld", reportDTO.getCampaignId());
        params.put("creativeld", reportDTO.getContentId());
        params.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        params.put("oaidSum", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        params.put("imeiSum", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? SecureUtil.md5(reportDTO.getImeiSum()) : "");
        params.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        params.put("ip", reportDTO.getIp());
        params.put("os", "android");
        params.put("time", System.currentTimeMillis());
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callbackUrl=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportClickUrl() + "&" + urlParams;
    }


    public static String builderJdjrUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("platform", "android");
        map.put("ts_ms", reportDTO.getTraceTime());
        map.put("click_id", reportDTO.getBizRequestId());
        map.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        map.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        map.put("oaidSum", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        map.put("android_id_sum", "");
        map.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        map.put("callback_url", allCabackUrl);
        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + params;
    }
}